package com.yoomiito.app.ui.order.list

import android.os.Bundle

import androidx.fragment.app.Fragment
import android.view.View

import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.adapter.ViewPagerFragmentAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.utils.*

import java.util.ArrayList

import kotlinx.android.synthetic.main.act_order.*
import kotlinx.android.synthetic.main.title_bar.*

class OrderActivity : BaseActivity<OrderPresenter>() {
    private val mFragments = ArrayList<Fragment>()
    private val types = mutableListOf(1, 2, 3, 4, 5)
    private val tabs = mutableListOf("已提交", "被驳回", "待发货", "待收货", "已完成")
    private var tabSelect = 0
    var mType: Int = 0 //用于请求返回数据
    var select: Int = 0 //用于请求返回数据

    override fun getLayoutId(): Int = R.layout.act_order

    override fun newP(): OrderPresenter {
        return OrderPresenter(App.mAppConfig)
    }

    override fun initData(savedInstanceState: Bundle?) {
        select = intent.getIntExtra("select", 0)
        mType = intent.getIntExtra(Constant.TYPE, -1)
//        when (intent.getIntExtra(Constant.TYPE, Type.GoodsType.TaoBao)) {
//            Type.GoodsType.GiveMore-> tabSelect = 4
//            Type.GoodsType.FreeGift-> tabSelect = 5
//            Type.GoodsType.TaoBao -> tabSelect = 0
//            Type.GoodsType.Gift -> tabSelect = 6
//            Type.GoodsType.BaoPin -> tabSelect = 3
//            Type.GoodsType.YouPin -> tabSelect = 2
//            Type.GoodsType.College -> tabSelect = 7
//            Type.GoodsType.StarStore -> tabSelect = 8
//            Type.GoodsType.JingDong->tabSelect = 1
//        }
        tv_center.visibility = View.VISIBLE
        tv_center.text = if (mType == 1) "我的订单" else "客户订单"
        iv_back_left.setOnClickListener { finish() }
        initTabLayout()
        initViewPager()
    }

    private fun initViewPager() {
        if (UserInfoConfig.getCompanyId() == 3) {
            types.add(Type.GoodsType.StarStore)
            tabs.add("星店")
        }
        mFragments.clear()
        if(mType == 1){
            for (i in tabs.indices) {
                val orderFragment = OrderFragment2()
                val bundle = Bundle()
                bundle.putInt(Constant.TYPE, types[i])
                bundle.putInt(Constant.KEY, mType)
                orderFragment.arguments = bundle
                mFragments.add(orderFragment)
            }
        }else{
            val orderFragment = OrderFragment2()
            val bundle = Bundle()
            bundle.putInt(Constant.TYPE, 1)
            bundle.putInt(Constant.KEY, mType)
            orderFragment.arguments = bundle
            mFragments.add(orderFragment)
        }
        act_order_vp!!.adapter = ViewPagerFragmentAdapter(supportFragmentManager, mFragments)
        for (i in tabs.indices) {
            act_order_tabLayout.addTab(act_order_tabLayout.newTab().setText(tabs[i]))
        }
        act_order_tabLayout.setupWithViewPager(act_order_vp)
        for (i in tabs.indices) {
            act_order_tabLayout.getTabAt(i)?.text = tabs[i]
        }
        act_order_tabLayout.getTabAt(tabSelect)?.select()
        act_order_vp.currentItem = select
        if(mType == 2)
            act_order_tabLayout.visibility = View.GONE
    }

    private fun initTabLayout() {

        //        mTabLayout.post(() -> TabLayoutUtils.setIndicator(mTabLayout, 24, 24));
    }
}
